<script setup lang="ts">
import { ref, watch } from "vue";
import { message } from "@/utils/message";
import { FormInstance } from "element-plus";
import { addOrUpdateElevator } from "@/api/sysele";

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => {
      return {};
    }
  },
  bmdata: {
    type: Object,
    default: () => {
      return [];
    }
  }
});

const ruleFormRef = ref<FormInstance>();

const formVisible = ref(false);
const formData = ref(props.data);

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(valid => {
    if (valid) {
      addOrUpdateElevator(formData.value)
        .then(res => {
          if (res.success) {
            message("提交成功", { type: "success" });
            formVisible.value = false;
            resetForm(formEl);
            emit("add-up-search"); //用emit来进行事件通知传递
          } else {
            message("提交失败:" + res.errMsg, { type: "error" });
          }
        })
        .catch(error => {
          message("提交失败:" + JSON.stringify(error), { type: "error" });
        });
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const closeDialog = () => {
  formVisible.value = false;
  resetForm(ruleFormRef.value);
};

const emit = defineEmits(["update:visible", "add-up-search"]);
watch(
  () => formVisible.value,
  val => {
    emit("update:visible", val);
  }
);

watch(
  () => props.visible,
  val => {
    formVisible.value = val;
  }
);

watch(
  () => props.data,
  val => {
    formData.value = val;
  }
);

const rules = {
  manageid: [{ required: true, message: "请选择小区", trigger: "blur" }],
  tung: [{ required: true, message: "请输入楼栋号", trigger: "blur" }],
  unit: [{ required: true, message: "请输入单元号", trigger: "blur" }],
  name: [{ required: true, message: "请输入电梯号", trigger: "blur" }],
  maxfloor: [{ required: true, message: "请输入最高楼层", trigger: "blur" }],
  elevatorid: [{ required: true, message: "请输入电梯编码", trigger: "blur" }]
};
</script>

<template>
  <el-dialog
    v-model="formVisible"
    title="编辑电梯信息"
    :width="680"
    draggable
    :before-close="closeDialog"
  >
    <!-- 表单内容 -->
    <el-form
      ref="ruleFormRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="小区" prop="manageid">
        <el-select
          v-model="formData.manageid"
          filterable
          placeholder="请选择小区"
          clearable
          class="!w-[160px]"
        >
          <el-option
            :label="item.fullname"
            :value="item.id"
            v-for="item in props.bmdata"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋号" prop="tung">
        <el-input
          v-model="formData.tung"
          :style="{ width: '480px' }"
          placeholder="请输入楼栋号"
        />
      </el-form-item>
      <el-form-item label="单元号" prop="unit">
        <el-input
          v-model="formData.unit"
          :style="{ width: '480px' }"
          placeholder="请输入单元号"
        />
      </el-form-item>
      <el-form-item label="电梯号" prop="name">
        <el-input
          v-model="formData.name"
          :style="{ width: '480px' }"
          placeholder="请输入电梯号"
        />
      </el-form-item>
      <el-form-item label="最高楼层" prop="maxfloor">
        <el-input
          v-model="formData.maxfloor"
          :style="{ width: '480px' }"
          placeholder="请输入最高楼层"
        />
      </el-form-item>
      <el-form-item label="电梯编码" prop="elevatorid">
        <el-input
          v-model="formData.elevatorid"
          :style="{ width: '480px' }"
          placeholder="请输入电梯编码"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
